﻿using GraduationProject.Model;
using GraduationProject.Webs.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GraduationProject.Webs.Controllers
{
    public class CommentController : BaseController
    {
        #region 字段
        public readonly GraduationProject.RDBMS.Comment _commentService = new RDBMS.Comment();
        #endregion

        // 文章详情页面
        public ActionResult Index(int NovelID)
        {
            SortList[] Sort;
            //获取分类标记数据
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TrySelectSortList(out Sort, null))
                {

                    ViewData["SortModel"] = Sort.ToList();
                }
            }
            int[] NovelIDs = new int[] { NovelID };
            bool a;
            //增加点击量
            using (var conn = new RDBMS.Novel())
            {
                if (CurrentUser!=null)
                {
                    conn.TryAddClickNum(out a, NovelID, CurrentUser.UserID);
                }
                conn.TryAddClickNum(out a, NovelID, 1);
            }
            Novel[] novels;
            Comment[] comments;
            var model = new NovelModel();
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovel(out novels, NovelIDs))
                {
                    if (novels.Length > 0)
                    {
                        model.novelModel = novels[0];
                    }
                }
            }
            using (var conn = new RDBMS.Comment())
            {
                if (conn.TrySelectCommentByNovelID(out comments, NovelID))
                {
                    if (comments.Length > 0)
                    {
                        model.CommentItems = comments.ToList() ;
                    }
                }
            }
            ViewData["UserModel"] = CurrentUser;
            //获取文章分页数据
            return View(model);
        }

        /// <summary>
        /// 获取评论内容(根据文章的编号)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetCommentData(int NovelID)
        {
            Comment[] Comments;
            var UserInfoList = _commentService.TrySelectCommentByNovelID(out Comments, NovelID);
            if (UserInfoList)
            {
                return Json(new { code = 0, msg = "成功", count = Comments.ToList().Count, data = UserInfoList });
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<Comment>() });
        }
        /// <summary>
        /// 回复插入
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Insert(Comment model)
        {
            model.CreateTime = DateTime.Now;
            model.AgreeNum = 0;
            model.DisagreeNum = 0;
            if (CurrentUser == null)
            {
                return Json(new { code = 1, msg = "你未登陆，不能进行回复！" });
            }
            model.CreateUser = CurrentUser.UserID;
            var models = new Comment[] { model };
            bool result;
            using (var conn = new RDBMS.Comment())
            {
                if (conn.TryInsertComment(out result, models))
                {
                    if (result)
                    {
                        return Json(new { code = 0, msg = "回复成功" });
                    }
                }
            }
            return Json(new { code = 1, msg = "回复失败，请稍后重试！" });
        }

        /// <summary>
        /// 评论点赞
        /// </summary>
        /// <param name="NovelID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CommAddAgreeNum(int CommentID)
        {
            if (CurrentUser == null)
            {
                return Json(new { code = 1, msg = "你未登陆，无法进行点赞！" });
            }
            bool result;
            using (var conn = new RDBMS.Comment())
            {
                if (conn.TryAddAgreeNum(out result, CommentID, CurrentUser.UserID))
                {
                    if (result)
                    {
                        return Json(new { code = 0, msg = "评论点赞成功" });
                    }
                    else
                    {
                        return Json(new { code = 0, msg = "已经点赞，不可重复点击" });
                    }
                }
            }
            return Json(new { code = 1, msg = "点赞失败，请稍后重试！" });
        }
    }
}